package cn.edu.lcu.cs.architecture.event.demo;

import java.time.Instant;
import java.util.Random;

import static cn.edu.lcu.cs.architecture.event.demo.Event.Type.MOUSE;

/**
 * 鼠标事件源
 */
public class Mouse extends EventSource {
    private String[] messages = {"【单击】", "【双击】", "【移动】"};

    public Mouse(EventDispatcher dispather) {
        super(dispather);
    }

    private String generateMouseMessage() {
        Random random = new Random();
        int index = random.nextInt(messages.length);
        return messages[index];
    }

    @Override
    public void run() {
        while (true) {
            // 模拟产生一次事件
            Event event = new Event(MOUSE, generateMouseMessage() + "\t" + Instant.now());
            // 添加到事件分发器的队列中
            dispather.enQueue(event);
            sleep(); // 随机休眠若干毫秒
        }
    }
}
